import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表盘', icon: 'Odometer', requiresAuth: true }
      },

    ]
  },
  {
    path: '/purchase',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/purchase/orders',
    meta: { title: '采购管理', icon: 'ShoppingCart', requiresAuth: true },
    children: [
      {
        path: 'orders',
        name: 'PurchaseOrders',
        component: () => import('@/views/purchase/Orders.vue'),
        meta: { title: '采购订单', icon: 'Document', requiresAuth: true, permission: 'purchase:order:list' }
      },
      {
        path: 'suppliers',
        name: 'Suppliers',
        component: () => import('@/views/purchase/Suppliers.vue'),
        meta: { title: '供应商管理', icon: 'User', requiresAuth: true, permission: 'purchase:supplier:list' }
      },
      {
        path: 'materials',
        name: 'Materials',
        component: () => import('@/views/purchase/Materials.vue'),
        meta: { title: '物料管理', icon: 'Box', requiresAuth: true, permission: 'purchase:material:list' }
      }
    ]
  },
  {
    path: '/warehouse',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/warehouse/inventory',
    meta: { title: '仓库管理', icon: 'House', requiresAuth: true },
    children: [
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import('@/views/warehouse/Inventory.vue'),
        meta: { title: '库存管理', icon: 'Box', requiresAuth: true, permission: 'warehouse:inventory:list' }
      },
      {
        path: 'inbound',
        name: 'Inbound',
        component: () => import('@/views/warehouse/Inbound.vue'),
        meta: { title: '入库管理', icon: 'ArrowDown', requiresAuth: true, permission: 'warehouse:inbound:list' }
      },
      {
        path: 'outbound',
        name: 'Outbound',
        component: () => import('@/views/warehouse/Outbound.vue'),
        meta: { title: '出库管理', icon: 'ArrowUp', requiresAuth: true, permission: 'warehouse:outbound:list' }
      },
      {
        path: 'permissions',
        name: 'WarehousePermissions',
        component: () => import('@/views/system/Permissions.vue'),
        meta: { title: '权限管理', icon: 'Lock', requiresAuth: true, permission: 'warehouse:permission:list' }
      }
    ]
  },
  {
    path: '/sales',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/sales/orders',
    meta: { title: '销售管理', icon: 'Sell', requiresAuth: true },
    children: [
      {
        path: 'orders',
        name: 'SalesOrders',
        component: () => import('@/views/sales/Orders.vue'),
        meta: { title: '销售订单', icon: 'Document', requiresAuth: true, permission: 'sales:order:list' }
      },
      {
        path: 'customers',
        name: 'Customers',
        component: () => import('@/views/sales/Customers.vue'),
        meta: { title: '客户管理', icon: 'User', requiresAuth: true, permission: 'sales:customer:list' }
      },
      {
        path: 'products',
        name: 'Products',
        component: () => import('@/views/sales/Products.vue'),
        meta: { title: '产品管理', icon: 'Goods', requiresAuth: true, permission: 'sales:product:list' }
      }
    ]
  },
  {
    path: '/finance',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/finance/accounts',
    meta: { title: '财务管理', icon: 'Money', requiresAuth: true },
    children: [
      {
        path: 'accounts',
        name: 'Accounts',
        component: () => import('@/views/finance/Accounts.vue'),
        meta: { title: '账户管理', icon: 'Wallet', requiresAuth: true, permission: 'finance:account:list' }
      },
      {
        path: 'invoices',
        name: 'Invoices',
        component: () => import('@/views/finance/Invoices.vue'),
        meta: { title: '发票管理', icon: 'Document', requiresAuth: true, permission: 'finance:invoice:list' }
      },
      {
        path: 'reports',
        name: 'Reports',
        component: () => import('@/views/finance/Reports.vue'),
        meta: { title: '财务报表', icon: 'PieChart', requiresAuth: true, permission: 'finance:report:list' }
      }
    ]
  },
  {
    path: '/retail',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/retail/products',
    meta: { title: '零售管理', icon: 'Shop', requiresAuth: true },
    children: [
      {
        path: 'products',
        name: 'RetailProducts',
        component: () => import('@/views/retail/Products.vue'),
        meta: { title: '商品管理', icon: 'Goods', requiresAuth: true, permission: 'retail:product:list' }
      },
      {
        path: 'stores',
        name: 'Stores',
        component: () => import('@/views/retail/Stores.vue'),
        meta: { title: '门店管理', icon: 'House', requiresAuth: true, permission: 'retail:store:list' }
      },
      {
        path: 'orders',
        name: 'RetailOrders',
        component: () => import('@/views/retail/Orders.vue'),
        meta: { title: '零售订单', icon: 'Document', requiresAuth: true, permission: 'retail:order:list' }
      },
      {
        path: 'members',
        name: 'Members',
        component: () => import('@/views/retail/Members.vue'),
        meta: { title: '会员管理', icon: 'User', requiresAuth: true, permission: 'retail:member:list' }
      },
      {
        path: 'inventory',
        name: 'RetailInventory',
        component: () => import('@/views/retail/Inventory.vue'),
        meta: { title: '零售库存', icon: 'Box', requiresAuth: true, permission: 'retail:inventory:list' }
      }
    ]
  },
  {
    path: '/system',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/system/users',
    meta: { title: '系统管理', icon: 'Setting', requiresAuth: true },
    children: [
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/system/Users.vue'),
        meta: { title: '用户管理', icon: 'User', requiresAuth: true, permission: 'system:user:list' }
      },
      {
        path: 'roles',
        name: 'Roles',
        component: () => import('@/views/system/Roles.vue'),
        meta: { title: '角色管理', icon: 'Avatar', requiresAuth: true, permission: 'system:role:list' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  
  const userStore = useUserStore()
  const token = userStore.token
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - ERP系统` : 'ERP系统'
  
  if (to.meta.requiresAuth) {
    if (!token) {
      next('/login')
      return
    }
    
    // 临时禁用权限检查，方便调试
    // if (to.meta.permission && !userStore.hasPermission(to.meta.permission)) {
    //   ElMessage.error('没有访问权限')
    //   next(from.path || '/dashboard')
    //   return
    // }
  }
  
  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router 